package com.jfinal.aceadmin.model;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import com.jfinal.aceadmin.vo.FollowInfoVo;
import com.jfinal.plugin.activerecord.Db;
@Repository
public class FollowImpl{

	private static ApplicationContext ctx;

	public static FollowImpl getDAO() {
		if (null == ctx) {
			ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		}
		FollowImpl instance = ctx.getBean(FollowImpl.class);
		return instance;
	}

	public List<Follow> getUserFollowers(int userId) {

		List<Follow> followers = Follow.dao.find(
				"SELECT * FROM Follow WHERE userId = ?", userId);
		return followers;
	}
	
	public List<Follow> getUserFollowings(int userId){
		List<Follow> followers = Follow.dao.find(
				"SELECT * FROM Follow WHERE followerId = ?", userId);
		return followers;
	}

	public FollowInfoVo getUserFollowInfo(int userId) {
		int followingsCount = Db.queryLong("SELECT COUNT(*) FROM Follow WHERE followerId = ?",userId).intValue();
		int followersCount = Db.queryLong("SELECT COUNT(*) FROM Follow WHERE userId = ?",userId).intValue();
		FollowInfoVo followInfoVo = new FollowInfoVo();
		followInfoVo.setFollowers(followersCount);
		followInfoVo.setFollowings(followingsCount);
		return followInfoVo;
	}
	
}
